package dao;

import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.Query;

public class FederationDAO {
	private EntityManager em;
	
	public FederationDAO(EntityManager em) {
		this.em = em;
	}
	
	public void updateEntityManager(EntityManager em){
		this.em = em;
	}
	
	public ArrayList<Federation> searchAllFederation(){
		Query query = em.createNamedQuery("searchAllFederation");

		ArrayList<Federation> colFederation = (ArrayList<Federation>)query.getResultList();
		return colFederation;
	}
	
//	public Federation searchFederation(String federation){
//		Federation federationSearch;
//		try{
//			int codigo = Integer.parseInt(federation);
//			federationSearch = searchCode(codigo);
//			if(federationSearch == null){
//				federationSearch = searchName(federation);
//			}
//		}catch(Exception e){
//			federationSearch = searchName(federation);
//		}
//		
//		return federationSearch;
//	}
//	
//	public Federation searchCode(int federation){
//		Query query = em.createNamedQuery("searchFederationCode");
//	    query.setParameter("code",federation);
//	    return (Federation) query.getSingleResult();
//	}
//	
//	public Federation searchName(String name){
//		Query query = em.createNamedQuery("searchFederationName");
//	    query.setParameter("name", name + "%");
//	    
//	     ArrayList<Federation> colFederation = (ArrayList<Federation>)query.getResultList();
//	    
//	    if(colFederation.size() == 1){
//	    	return colFederation.get(0);	    	
//	    }else {
//	    	DlgConsultFederation selFederation = new DlgConsultFederation(colFederation);
//	    	
//	    	return selFederation.getFederation();
//	    }
//	}
//	public ArrayList<Federation> searchNameDirect(String federation){
//		Query query = em.createNamedQuery("searchFederationName");
//	    query.setParameter("name", federation + "%");
//	    
//	    ArrayList<Federation> colFederation = (ArrayList<Federation>)query.getResultList();
//	    return colFederation;
//	}
//	
//	public boolean save(Federation federation){
//		EntityTransaction trans = em.getTransaction();
//		trans.begin();
//
//		try{
//			if (em.find(Federation.class,federation.getOID()) != null){
//				em.merge(federation);
//			}else {
//				em.persist(federation);
//			}
//			trans.commit();
//		}catch (Exception e) {
//			e.printStackTrace();
//			trans.rollback();
//			return false;
//		}
//		
//		em.close();
//		DAOFactory.getInstance().updateEntityManager();
//		
//		return true;
//	}	
}








